<?php
/*
 * Created on 29.01.2006
 * 
 * config:
 * target       target to redirect to (optional)
 * add-slash    should a slash be added to url (default is true)
 */

m2import('phpinchains/Filter.php');

class RedirectRouteFilter extends phpinchains_Filter
{
    
    var $config;
    var $slash;

    function doFilter (env $env, $chain)
    {
        foreach ($this->config as $n => $v) {
            if ($n != 'root-path' && $chain->match($n)) {
                $lastMatches = $chain->getLastMatches();
                if (!is_array($v) && preg_match_all('/\[(\d+)\]/', $v, $matches)) {
                    for ($i = 0; $i < count($matches[0]); $i++) {
                        $v = str_replace($matches[0][$i], $lastMatches[(int) $matches[1][$i]], $v);
                    }
                }
                if (preg_match('#^http://#', $v)) {
                    header('Location: '.$v);
                } else {
                    header('Location: '.$this->config['root-path'].$v);
                }
                return;
            }
        }
        $chain->doFilter($env);
    }
    
    function init (& $config)
    {
        $this->config = & $config;
    }

}   